import * as API from '../../api/users.js'

const state={
    loginuser:{},//用户登录后信息
    loginuserMsg:{},
    // attention:{}
}

const getters={  //计算属性
    history(state){
        //console.log(sessionStorage.getItem('userId'))
        if(sessionStorage.getItem('userId')){
          
            let newInfo = state.loginuserMsg.viewing.cont.sort((a,b)=>{return b.time - a.time})
            return newInfo
        }else{
            let newInfo = []
            return newInfo
        }
        
    }
}

const mutations={  // 同步
    usersMsg(state,data){
        if (data.code===200) {
            // console.log(data.info[0] );
           state.loginuser=data.info[0] 
        }
        
    },
    clear(state,data){
        state.loginuser={}
        state.loginuserMsg={}
    },
    getUser(state,res){
        // console.log(res)
        state.loginuser=res.info.user
        res.info.viewing.cont.forEach((item,index)=>{
            item.check  = false
        })
        state.loginuserMsg=res.info
        // console.log(state.loginuserMsg.viewing);
    }
}
const actions={   // 异步
    login({commit},data){
        return new Promise((resolve,rej)=>{
             API.login(data).then((res)=>{
                 commit('usersMsg',res)
                 resolve(res)
             })
        })
    },
    eidt({commit},data){
        return new Promise((resolve,rej)=>{
            API.eidt(data).then((res)=>{
                resolve(res)
            })
        })
        
    },
    deletuser({commit},data){
        return new Promise((resolve,rej)=>{
            API.deletuser(data).then((res)=>{
                resolve(res)
            })
        })
        
    },
    getUser({commit},data){
        return new Promise((resolve,rej)=>{
            API.getUser(data).then((res)=>{
                
                commit('getUser',res)
                resolve(res)
            })
        })
        
    },

}

export default {
    namespaced:true,
    state,
    getters,
    mutations,
    actions
}

